<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class model extends CI_Controller {
    
	function __construct()
	{
		parent::__construct();
		$this->load->model("mod");
		$this->load->model("version");
		$this->load->model("complete");
	    $this->load->model("category");
	    $this->load->model("company");
	    $this->load->model("receive");
	    $this->load->model("receive_record");
	    $this->load->model("receive_update");
	    $this->load->model("send");
	    $this->load->model("send_record");
	    $this->load->model("send_update");
	    $this->load->model("inventory");
		$this->load->model("admin");
		$this->load->helper('fusionCharts');
		
	}

	function chart(){
	
	$this->load->view('chart');
	}
	
	function getcategory(){
		  $mod_id = $_POST['mod_id'];
	      $result = $this->category->getcategory_bymod_id($mod_id);
	      $count = count($result);
	      $str="";
	      if($count!=0){
	         for($i=0;$i<$count;$i++){
	           $str.="<p style='width:190px;float:left;'><input type='checkbox' name='category_ids[]' value='".$result[$i]->category_id."' />".$result[$i]->name." </p>";
	         }
	         echo $str;
	      }else{
	        echo "暂无种类";
	      }
	}
	
	function get_inventory_for_layout(){
	    $inventory_id = $_POST['inventory_id'];
	    $inventory = $this->inventory->select_by_id($inventory_id);
	    $mod = $this->mod->select($inventory[0]->mod_id);
	    $category = $this->category->select(array('category_id'=>$inventory[0]->category_id));
	    $pre_version = $this->version->getversion_array(array('version_id'=>$inventory[0]->version_id));
	    $version = $this->version->getversion_array(array('category_id'=>$inventory[0]->category_id));
	    $str = "";
	    foreach($version as $k=>$v){
	        $str.="<option value='".$v->version_id."'";
	        if($inventory[0]->version_id==$v->version_id){
	          $str.="  selected=true  ";
	        }
	        $str.=">".$v->name."</option>";
	    }
	    $array = array(
	                    'mod'=>$mod[0]->name,
	                    'category'=>$category[0]->name,
	                    'count'=>$inventory[0]->count,
	                    'version' => $str,
	                    'pre_verison'=>$pre_version[0]->name,
	                    'mod_id'=>$inventory[0]->mod_id,
	                    'category_id'=>$inventory[0]->category_id,
	                    'inventory_id'=>$inventory_id,
	                    'pre_version_id'=>$pre_version[0]->version_id,
	    );
	    $arr = json_encode($array);
	    echo $arr;
	    
	}
	
	
	function update_inventory(){
	    $inventory_id = $_POST['inventory_id'];
		$mod_id = $_POST['mod_id'];
		$category_id = $_POST['category_id'];
		$version_id = $_POST['version_id'];
		$count = $_POST['count'];
		$codes = $_POST['codes'];
		$pre_version = $_POST['pre_version'];
		$pre_version_id = $_POST['pre_version_id'];
		$pre_count = $_POST['pre_count'];	
		
		$user = $this->session->userdata('user');
		if($count>$pre_count){
		   echo '更新库存数量不能大于'.$pre_count;exit;
		}else {

		  $inven = array(
		               'mod_id'=>$mod_id,
		               'category_id'=>$category_id,
		               'version_id'=>$version_id,
		               'count'=>$count		  
		  );
		  $result = $this->inventory_manage($inven);
//新增库存后，要新增一条接收记录
          $data = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id,'count'=>$count,'createtime'=>time(),'codes'=>$codes,'contacter'=>$user[0]->admin_id);
          $r_id = $this->receive->insert($data);
          $data['is_update'] = 1;
          $data['r_id'] = $r_id;
          $this->receive_record->insert($data);
                    
		  if($result){
//更新原来的那个库存
            $in = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$pre_version_id,'count'=>'-'.$count);
            $this->inventory_manage($in);		  	
                  $arrr = array(
	                  'mod_id'=>$mod_id,
	                  'category_id'=>$category_id,
	                  'version_id'=>$version_id,
	              	  'count'=>$count,
	              	  'pre_count'=>$pre_count,
	              	  'pre_version'=>$pre_version_id,
	              	  'codes'=> $codes	
                  );	
              $ss = $this->receive_update->insert($arrr);  
              if($ss){
              	echo '1';
              }else{
                echo '操作失败！';
              }    
		  }
		}
	
	}
	
	
	function get_receive_for_layout(){
	    $inventory_id = $_POST['inventory_id'];
	    $receive = $this->inventory->select(array('inventory_id'=>$inventory_id));
	    $mod = $this->mod->select($receive[0]->mod_id);
	    $category = $this->category->select(array('category_id'=>$receive[0]->category_id));
	    $version = $this->version->getversion_array(array('category_id'=>$receive[0]->category_id));
	    $current_version = $this->version->getversion_array(array('version_id'=>$receive[0]->version_id));
	    $str = "";
	    foreach($version as $k=>$v){
	        $str.="<option value='".$v->version_id."'";
	        if($receive[0]->version_id==$v->version_id){
	          $str.="  selected=true  ";
	        }
	        $str.=">".$v->name."</option>";
	    }
	    $array = array(
	                    'mod'=>$mod[0]->name,
	                    'category'=>$category[0]->name,
	                    'version' => $str,
	                    'current_version'=>$current_version[0]->name,
	                    'count'=>$receive[0]->count,
	                    'pre_verison'=>$receive[0]->version_id,
	                    'mod_id'=>$receive[0]->mod_id,
	                    'category_id'=>$receive[0]->category_id,
	                    'inventory_id'=>$inventory_id
	    );
	    $arr = json_encode($array);
	    echo $arr;
	    
	}
	
	
	function update_receive(){
	    $receive_id = $_POST['receive_id'];
		$mod_id = $_POST['mod_id'];
		$category_id = $_POST['category_id'];
		$version_id = $_POST['version_id'];
		$count = $_POST['count'];
		$codes = $_POST['codes'];
		$pre_version = $_POST['pre_version'];
		$pre_count = $_POST['pre_count'];
		$pre_count_record = $_POST['pre_count_record'];
		$user = $this->session->userdata('user');
		if($count>$pre_count_record){
		  echo '更新份数不能高于'.$pre_count_record;exit;
		}

     //版次不变，则更新库存
        if($version_id == $pre_version){
	         $resu = $this->receive->update($receive_id);
	         $update = array();
//	         $resu = $this->receive_update->insert($receive_id); 
		     if($resu){
		      $pre_count = $_POST['pre_count'];
		      $after_count = $_POST['count'];
		      $min = $after_count-$pre_count; 
		      $array = array('mod_id'=>$_POST['mod_id'],'category_id'=>$_POST['category_id'],'version_id'=>$_POST['version_id'],'count'=>$min);	
	          $this->inventory_manage($array);
	  	     }
        }else{
    //版次发生变化,先判断是否已存在更改后的版次，如果已经存在，则首页合并，后台新增一条接收数据，同时更改库存
              $arr = array('mod_id'=>$_POST['mod_id'],'category_id'=>$_POST['category_id'],'version_id'=>$_POST['version_id']);
              $result = $this->inventory->select_by_array($arr); 
              $count_record = count($result);
              if($count_record>0){
                 $c = $result[0]->count+$count;
                 $this->inventory->update($result[0]->inventory_id,array('count'=>$c));
              }else{
                 $arr['count'] = $count;
                 $this->inventory->insert($arr);
              }
                $ar = array('mod_id'=>$_POST['mod_id'],'contacter'=>$user[0]->admin_id,'category_id'=>$_POST['category_id'],'version_id'=>$_POST['version_id'],'count'=>$count,'codes'=>$codes,'createtime'=>time());
                $r_id = $this->receive->insert($ar);  
                $ar['r_id'] = $r_id;
                //$ar['is_update'] = 1;
                $rs = $this->receive_record->insert($ar); 
                $con = array('mod_id'=>$_POST['mod_id'],'category_id'=>$_POST['category_id']);             
                $this->receive_record->update_array($con,$_POST['version_id'],array('is_update'=>1));
              
          //更新原来的接收记录 和两个版次的库存
              if($rs){ 
                  $arrr = array('mod_id'=>$_POST['mod_id'],'category_id'=>$_POST['category_id'],'version_id'=>$_POST['version_id']);
              	  $arrr['count']=$count;
              	  $arrr['pre_count'] = $pre_count;
              	  $arrr['pre_version'] = $pre_version;
              	  $arrr['codes'] = $codes;
              	  $this->receive_update->insert($arrr);
	              $c_s = array('count'=>$pre_count-$count,'other_codes'=>$_POST['pre_codes'].",".$_POST['codes']);
	              
	              if($pre_count-$count<=0){
	              	  $this->receive->del($receive_id);
	              }
	           //原库存减少   
	              $array = array('mod_id'=>$_POST['mod_id'],'category_id'=>$_POST['category_id'],'version_id'=>$pre_version);	
	              $left = $this->inventory->select_by_array($array);
	              $left_count = $left[0]->count-$count;
	              $this->inventory->update($left[0]->inventory_id,array('count'=>$left_count));   
              }
        }
        echo '1';exit();	
	}
	
	
	function update_send(){
	    $send_id = $_POST['send_id'];
		$mod_id = $_POST['mod_id'];
		$category_id = $_POST['category_id'];
		$version_id = $_POST['version_id'];
		$company_id = $_POST['company_id'];
		$count = $_POST['count'];
		$pre_version = $_POST['pre_version'];
		$pre_count = $_POST['pre_count'];
		$des = $_POST['des'];
		$codes = $_POST['codes'];
		$user = $this->session->userdata('user');
		if($count>$pre_count){
		  echo "资料份数不能高于".$pre_count;exit();
		}
	//取出现有库存 	
	         $array=array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id);
	         $resu = $this->inventory->select($array);
	         if(count($resu)==0){
	           $co=0;
	         }else{
	           $co = $resu[0]->count;
	         }
	         
	         if($count>$co){
	           echo "库存份数不够";	exit();         
	         }
	         
	//版次一样,数量不同
	    if($pre_version==$version_id){
	      if($pre_count!=$count){
	           $cc = $pre_count-$count;
	           $array['count']=$cc;
	           $result = $this->inventory_manage($array);
	           if($result){
	           	   unset($array['count']);
		           $array['count'] = $count;
		           $array['company_id'] = $company_id;
		           $array['des'] = $_POST['des'];
		           $array['other_codes'] = $_POST['pre_codes'].",".$_POST['codes'];
		           $array['createtime'] = time();
		           $array['s_id'] = $send_id;
		           $resu =$this->send_record->insert($array);
		   //新增完后，被更新记录被冻结
		           $this->send_record->update($send_id,array('is_update'=>1));        
		           
		           unset($array['other_codes']);
		           unset($array['s_id']);
		           $resu =$this->send->update($send_id,$array);
		           $array['pre_version'] = $pre_version;
		           $array['pre_count'] = $pre_count;
		           $this->send_update->insert($array);
	           }
	           if($resu){
	             echo '1';
	           }
	      }
	      
	    }else if($pre_version!=$version_id){
    //根据新传过来的数据，先判断是否已经给这个公司发放过新版本资料，如果已发放过，则数量累加，如果没有，则新增记录  		
	      $arr = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id,'company_id'=>$company_id); 
	      $arra = $this->send->select_by_array($arr);
	      $count_record = count($arra);
	       $a = array('mod_id'=>$mod_id,'user_id'=>$user[0]->admin_id,'category_id'=>$category_id,'version_id'=>$version_id,'company_id'=>$company_id,'codes'=>$_POST['codes'],'count'=>$count,'createtime'=>time());
          if($count_record!=0){
                  $c = $arra[0]->count+$count;
                  $r = $this->send->update($arra[0]->send_id,array('count'=>$c));
                  $s_id = $arra[0]->send_id;
          }else{
	          //不存在发放记录		  	     		 
	     		  $s_id = $this->send->insert($a);	    	  
          }
         $a['is_update'] = 1; 
         //$a['s_id'] = $s_id;
         $res =$this->send_record->insert($a);	
         $ar = array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$pre_version,'company_id'=>$company_id); 
	     $this->send_record->update_array($ar,array('is_update'=>1)); 
	     		if($res){
	     		      $inven_array=array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id,'count'=>"-".$count);
		     		  $s=$this->inventory_manage($inven_array);
		     		//更新被更改记录
		     		  $ls_count = $pre_count-$count;
		     		  $ars = array('count'=>$ls_count);
		     		  $ars['updatetime']=time();
		     		  if( $ls_count<=0 ){
		     		   $result = $this->send->del($send_id);
		     		  }else{
		     		   $result = $this->send->update($send_id,array('count'=>$ls_count));
		     		  }
		     		    
		     		  $arr['count'] = $ls_count;
		     		  $arr['pre_version'] = $pre_version;
		              $arr['pre_count'] = $pre_count;
		              $arr['updatetime'] = time();
		              $this->send_update->insert($arr);
	     		  }
	     
	     	    echo '1';
	   
	     }
	//版次数量都不同
	}
	
	
	function get_send_for_layout(){
	    $send_id = $_POST['send_id'];
	    $send = $this->send->select($send_id);
	    $mod = $this->mod->select($send[0]->mod_id);
	    $category = $this->category->select(array('category_id'=>$send[0]->category_id));
	    $version = $this->version->getversion_array(array('category_id'=>$send[0]->category_id));
	    $current_version = $this->version->getversion_array(array('version_id'=>$send[0]->version_id));
	    $str = "";
	    foreach($version as $k=>$v){
	        $str.="<option value='".$v->version_id."'";
	        if($send[0]->version_id==$v->version_id){
	          $str.="  selected=true  ";
	        }
	        $str.=">".$v->name."</option>";
	    }
	    $array = array(
	                    'mod'=>$mod[0]->name,
	                    'category'=>$category[0]->name,
	                    'version' => $str,
	                    'current_version'=>$current_version[0]->name,
	                    'count'=>$send[0]->count,
	                    'pre_verison'=>$send[0]->version_id,
	                    'des'=>$send[0]->des,
	                    'mod_id'=>$send[0]->mod_id,
	                    'category_id'=>$send[0]->category_id,
	                    'send_id'=>$send_id,
	                    'codes'=>$send[0]->codes,
	                    'company_id'=>$send[0]->company_id
	    );
	    $arr = json_encode($array);
	    echo $arr;
	    
	}


//用户管理列表
	function admin(){
		$this->top();
	    $total = $this->admin->select('');
	    $config['base_url'] =  site_url('model/admin/');
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 10; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start'] = $start;
		$data['admin'] = $this->admin->select('',$config['per_page'],$page);
		$this->load->view('admin',$data);
		
	}
	
	
    function admin_auth(){
       $username = $_POST['username'];
       $count = $this->admin->auth($username);
       echo $count;exit;
    }	

	//添加用户
	function admin_add(){
	  if($_POST){
	  	$resu = $this->admin->insert();
	  	if($resu){
	  	  redirect('model/admin');
	  	}
	  }else{	  	
	    $this->top();
		$this->load->view('admin_add');
	  }
	}
	
	
//重置用户密码
    function admin_reset(){
        $id = $this->uri->segment(3);
	    $resu = $this->admin->update_reset($id); 
	     if($resu){
	  	  redirect('model/admin');
	  	 }
    }	
	
	function admin_detail(){
	    $id = $this->uri->segment(3);
	    $admin = $this->admin->select($id);
	    $data['admin'] = $admin[0]; 
	    $this->top();
        $this->load->view('admin_detail',$data);
	 
	}

	//删除用户
	function admin_del(){
		$id = $this->uri->segment(3);
	    $resu = $this->admin->del($id); 
	     if($resu){
	  	  redirect('model/admin');
	  	 }
	}

	//编辑用户
	function admin_edit(){
	  $id = $this->uri->segment(3);
	  $user = $this->session->userdata('user');
	  if($_POST){
	     $resu = $this->admin->update($id); 
	     if($resu){
	       if($user[0]->role==1){
	       	 redirect('main/index');
	       }else{
	         redirect('model/admin');
	       }		  	  
	  	 }
	  }else{
	     
	     $admin = $this->admin->select($id);
	     $data['admin'] = $admin[0];
	     $data['id']=$id;
	     $this->top();
	     $this->load->view('admin_add',$data);
	  }
	}


	function auth(){
	   $type = $_POST['type'];
	   
	   if($type=="mod"){
	      $result = $this->mod->auth($_POST['name']);
	      
	   }else if($type=="category"){
	      $result = $this->category->auth($_POST['codes'],$_POST['mod_id']);
	   }else if($type=="company"){
	      $result = $this->company->auth($_POST['name']);
	   }else if($type=="version"){
	      $result = $this->version->auth($_POST['name'],$_POST['category_id']);
	   }
	   echo $result;exit();
	}
	
	
	function top(){
	    $user = $this->session->userdata('user');
		if($user!=""){
			$data['user'] = $user;
		}else{ 
			//直接跳转到登录页面
			redirect('main/login');exit;
		}
	    $data['module']=$this->uri->segment(1);
	    $data['template']=$this->uri->segment(2);
	    $data['level_data'] = array('机密','秘密','内部');
	    $this->load->view('top',$data);
	}
	
	function inventory(){
        $this->top();
        if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('inventory_mod_id',$_POST['mod_id']);
	    	$this->session->set_userdata('inventory_category_id',$_POST['category_id']);
	    }
	    if(isset($_POST['see'])){
	        $this->session->set_userdata('see',$_POST['see']);
	    }
	    $mod_id=$this->session->userdata('inventory_mod_id');
	    $see=$this->session->userdata('see');
	    $category_id=$this->session->userdata('inventory_category_id');
	    if($mod_id!=""){
	    	$where = array('mod_id'=>$mod_id);
	    	if($category_id!=""){
	    	  $where['category_id'] = $category_id;                                                    
	    	}
	    }else{
	        $where = "";
	    }
	    $data['see'] = $see;	    	
	    $data['mod_id'] = $mod_id;
	    $data['category_id'] = $category_id;
	    
        $data['mod'] = $this->mod->select('');
        $data['category'] = $this->category->select('');
        $data['version'] = $this->version->select('');
	    $total = $this->inventory->select($where);
	    $config['base_url'] =  site_url('model/inventory/');
	    
		$config['total_rows'] = count($total);
		if(isset($_POST['see_all'])&&($_POST['see_all']==1)){
		  $config['per_page'] = 20000000000; 
		  $data['see_all'] = 1;
		}else{
		  $data['see_all'] = 0;	
		  $config['per_page'] = 20; 
		}
		$config['num_links'] = 10; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		//取得每个种类的最新版次
		$sr = $this->version->get_last_verison(); 
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start'] = $start;
		$data['inventory'] = $this->inventory->select($where,$config['per_page'],$page);
		$cou = 0;
		for($i=0;$i<count($data['inventory']);$i++){
			$cou+=$data['inventory'][$i]->count;
		     for($j=0;$j<count($sr);$j++){
		     	
		         if($sr[$j]['category_id']==$data['inventory'][$i]->category_id){
		           if($sr[$j]['version_id']==$data['inventory'][$i]->version_id){
		           	$data['inventory'][$i]->update=0;
		           }else{
		           	$data['inventory'][$i]->update=1;
		           }
		         }
		     }
		}
		$data['cou'] = $cou;
		$this->load->view('inventory',$data);
	}
	
	
	function get_version_detail(){
	    $version = $this->inventory->select(array('version_id'=>$_POST['version_id'],'mod_id'=>$_POST['mod_id'],'category_id'=>$_POST['category_id']));
        if(count($version)==0){
          echo 0;
        }else{
	      echo $version[0]->count;
        }
	}
	
//型号资料列表
    function receive(){
        $this->top();
        if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('receive_mod_id',$_POST['mod_id']);
	    	$this->session->set_userdata('receive_category_id',$_POST['category_id']);
	    }
	    $mod_id=$this->session->userdata('receive_mod_id');
	    $category_id=$this->session->userdata('receive_category_id');
	    if($mod_id!=""){
	    	$where = array('mod_id'=>$mod_id,'category_id'=>$category_id);
	    }else{
	        $where = "";
	    }
	    	    	
	    $data['mod_id'] = $mod_id;
	    $data['category_id'] = $category_id;
	    
        $data['version'] = $this->version->select('');
        $data['mod'] = $this->mod->select('');
        $data['category'] = $this->category->select('');
        $data['admin'] = $this->admin->select('');
	    $total = $this->receive_record->select($where);
	    $config['base_url'] =  site_url('model/receive/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 10; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['receive'] = $this->receive_record->select($where,$config['per_page'],$page);
		$this->load->view('receive',$data);
    }	
    
     function get_category(){
         $mod_id = $_POST['mod_id'];
         $result = $this->category->getcategory_bymod_id($mod_id);
         $str="<option value=''>全部资料种类</option>";
         foreach( $result as $k=>$val ){
         	$str.="<option value='".$val->category_id."'>".$val->name."</option>";
         }
         echo $str;
     }
     
     function get_version(){
         $category_id = $_POST['category_id'];
         $result = $this->version->getversion_bycategory_id($category_id);
         $str="<option value=''>请选择版本</option>";
         foreach( $result as $k=>$val ){
         	$str.="<option value='".$val->version_id."'>".$val->name."</option>";
         }
         echo $str;
     }
     
     
     function inventory_manage($array){
         $mod_id=$array['mod_id'];
         $category_id = $array['category_id'];
         $version_id = $array['version_id'];
         $count = $array['count'];
     //先根据mod_id category_id version_id 查询表,判断数据存在与否,不存在执行插入,存在,执行update
         $array=array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id);
         $result = $this->inventory->select2($array);

         $num = count($result);
         if($num==0){
           $array['count']=$count;
           $r = $this->inventory->insert($array);
         }else{
      //如果存在此条数据,则拿出其count 与数组传过来的count进行累加，然后更新此条记录
           $left_count = $result[0]->count+$count;
           $arra = array('count'=>$left_count);
           if($left_count<=0){
           	$r = $this->inventory->del($result[0]->inventory_id);
           }else{
           	$r = $this->inventory->update($result[0]->inventory_id,$arra);
           }
           
         }
         return $r;
     }
     
//型号资料添加
     function receive_add(){
        if($_POST){
        	$user = $this->session->userdata('user');
        	foreach($_POST as $k=>$val){
        	    $key = explode('_', $k);
        	    if(count($key)==3){
        	    	
        	       if($key[0]=="version"){	
        	          $ver = $key[0]."_".$key[1]."_".$key[2];
        	          $count = "counts"."_".$key[1]."_".$key[2];
        	          $c = count($_POST[$ver]);
        	          for( $i=0;$i<$c;$i++){
        	           //从版本表里获取version_id
        	             $version = array('mod_id'=>$key[1],'category_id'=>$key[2],'name'=>$_POST[$ver][$i],'count'=>$_POST[$count][$i],'createtime'=>time());	
        	             $version_id = $this->version->get_id($version);
        	             $array = array('mod_id'=>$key[1],'category_id'=>$key[2],'version_id'=>$version_id,'count'=>$_POST[$count][$i],'codes'=>$_POST['codes'],'contacter'=>$user[0]->admin_id,'createtime'=>time());
        	             $receive_id = $this->receive->insert($array);
        	             $this->inventory_manage($array);
        	             $array['r_id'] = $receive_id;
        	             $this->receive_record->insert($array);        	             
        	          }
        	       }
        	    }
        	}   

		  redirect('model/receive');

        }else{
          $this->top();
          $data['version'] = $this->version->select('');
          $data['mod'] = $this->mod->select('');
          $data['category'] = $this->category->select('');
		  $this->load->view('receive_add',$data);
        }
     }
   
//修改型号资料
    function receive_edit(){
      $id = $this->uri->segment(3);
	  if($_POST){
	  	 $this->receive_record->update($id);
	     $resu = $this->receive->update($id); 
	     if($resu){
	      $pre_count = $_POST['pre_count'];
	      $after_count = $_POST['count'];
	      $min = $after_count-$pre_count; 
	      $array = array('mod_id'=>$_POST['mod_id'],'category_id'=>$_POST['category_id'],'version_id'=>$_POST['version_id'],'count'=>$min);	
          $this->inventory_manage($array);
	      redirect('model/receive');
	  	 }
	  }else{
	     
	     $receive = $this->receive_record->select(array('r_id'=>$id));
	     $data['receive'] = $receive[0];
	     $data['id']=$id;
	     $this->top();
	     $data['category'] = $this->category->select('');
	     $data['admin'] = $this->admin->login(array('role'=>0));
	     $data['mod'] = $this->mod->select('');
	     $data['version'] = $this->version->getversion_bycategory_id($receive[0]->category_id);
	     $this->load->view('receive_edit',$data);
	  }
    }

//删除型号资料	
	function receive_del(){
		$id = $this->uri->segment(3);
	    $resu = $this->receive->del($id); 
	     if($resu){
	  	  redirect('model/receive');
	  	 }
	}

//查看型号资料详情
    function receive_detail(){
        $id = $this->uri->segment(3);
	    $receive = $this->receive_record->select(array('r_id'=>$id));
	    $data['receive'] = $receive[0]; 
	    $data['mod'] = $this->mod->select('');
	    $data['admin'] = $this->admin->select('');
	    $data['category'] = $this->category->select('');
	    $data['version'] = $this->version->select('');
	    $this->top();
        $this->load->view('receive_detail',$data);
    }	

    
	
//型号资料列表
    function send(){
        $this->top();
        if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('send_mod_id',$_POST['mod_id']);
	    	$this->session->set_userdata('send_category_id',$_POST['category_id']);
	    	$this->session->set_userdata('send_company_id',$_POST['company_id']);
	    }
	    $mod_id=$this->session->userdata('send_mod_id');
	    $category_id=$this->session->userdata('send_category_id');
	    $company_id=$this->session->userdata('send_company_id');
	    $where = array();
	    if($mod_id!=""){
	    	$where = array('mod_id'=>$mod_id);
	    	if($category_id!=""){
	    	  $where['category_id'] = $category_id;
	    	}
	    }
    	 if($company_id!=""){
	    	  $where['company_id'] = $company_id;
	     }	    	
	    if(count($where)==0){
	      $where="";
	    }
	    $data['mod_id'] = $mod_id;
	    $data['category_id'] = $category_id;
	    $data['company_id'] = $company_id;
	    
        $data['mod'] = $this->mod->select('');
        $data['category'] = $this->category->select('');
        $data['version'] = $this->version->select('');
        $data['company'] = $this->company->select('');
        $data['admin'] = $this->admin->select('');
	    $total = $this->send_record->select($where);
	    $config['base_url'] =  site_url('model/send/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 10; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['send'] = $this->send_record->select($where,$config['per_page'],$page);
		$this->load->view('send',$data);
    }	
    
//型号资料添加
     function send_add(){
     	$user = $this->session->userdata('user');
        if($_POST){
        	
    //根据型号，类别取出所有版次和数量
         $mod_id = $_POST['mod_id'];
         $category_id = $_POST['category_id'];
         $version_id = $_POST['version_id'];
         $a = $_POST['count'];
         $codes = $_POST['codes'];
    //取当前库存，判断     
         $array=array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id);
         $resu = $this->inventory->select($array);
         if(count($resu)==0){
           $count=0;
         }else{
           $count = $resu[0]->count;
         }
         if($a>$count){
           echo "<script>alert('库存份数不够！');location.href='".base_url()."model/send_add';</script>";
         
         }else{
           $arr = array('count'=>$count-$a);
           $result = $this->inventory->update($resu[0]->inventory_id,$arr);
           if($result){
	           $array['codes'] = $codes;
	           $array['count'] = $a;
	           $array['company_id'] = $_POST['company_id'];
	           $array['des'] = $_POST['des'];
	           $array['createtime'] = time();
	           $array['user_id'] = $user[0]->admin_id;
	           
	           $s_id = $this->send->insert($array);	   
	           $array['s_id'] = $s_id;
	           $this->send_record->insert($array);        
           }           
         }
	        
	      redirect('model/send');
		 
        }else{
          $this->top();
          $data['mod'] = $this->mod->select('');
	      $data['category'] = $this->category->select('');
          $data['version'] = $this->version->select('');
          $data['company'] = $this->company->select('');
		  $this->load->view('send_add',$data);
        }
     }
   
//修改型号资料
    function send_edit(){
      $id = $this->uri->segment(3);

	  if($_POST){
	     $mod_id = $_POST['mod_id'];
         $category_id = $_POST['category_id'];
         $version_id = $_POST['version_id'];
         $pre_count = $_POST['pre_count'];
         $pre_version = $_POST['pre_version'];
         $a = $_POST['count'];
         $codes = $_POST['codes'];
    //取当前库存，判断     
         $array=array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$version_id);
         $resu = $this->inventory->select($array);
         if(count($resu)==0){
           $count=0;
         }else{
           $count = $resu[0]->count;
         }
         
         if($a>$count){
           echo "<script>alert('库存份数不够！');location.href='".base_url()."model/send_add'</script>";
         
         }else{
           
           $cc = $pre_count-$a;
           $array['count']=$cc;
           $result = $this->inventory_manage($array);
  //低版本回收
           $arr=array('mod_id'=>$mod_id,'category_id'=>$category_id,'version_id'=>$pre_version,'count'=>'-'.$cc);     
           $this->inventory_manage($arr);  
           
           if($result){
           	   unset($array['count']);
	           $array['codes'] = $codes;
	           $array['count'] = $a;
	           $array['company_id'] = $_POST['company_id'];
	           $array['des'] = $_POST['des'];
	           $array['createtime'] = time();
	           $this->send_record->update($id,$array);
	           $resu =$this->send->update($id,$array);	          
           }
         }
	     if($resu){
	  	  redirect('model/send');
	  	 }
	  }else{
	     
	     $send = $this->send_record->select(array('s_id'=>$id));
	     $data['send'] = $send[0];
	     $data['id']=$id;
	     $this->top();
	     $data['mod'] = $this->mod->select('');
	     $data['category'] = $this->category->select('');
	//根据mod，category 查询version     
	     $arr = array('mod_id'=>$send[0]->mod_id,'category_id'=>$send[0]->category_id);
	     $data['version'] = $this->version->getversion_array($arr);
	     $data['company'] = $this->company->select('');
	     $this->load->view('send_add',$data);
	  }
    }

//删除型号资料	
	function send_del(){
		$id = $this->uri->segment(3);
	    $resu = $this->send->del($id); 
	     if($resu){
	  	  redirect('model/send');
	  	 }
	}

//查看型号资料详情
    function send_detail(){
        $id = $this->uri->segment(3);
	    $send = $this->send_record->select(array('s_id'=>$id));
	    $data['send'] = $send[0]; 
	    $data['mod']= $this->mod->select($send[0]->mod_id);
	    $data['category']= $this->category->select(array('category_id'=>$send[0]->category_id));
	    $data['version'] = $this->version->select('');
	    $data['company'] = $this->company->select('');
	    $this->top();
        $this->load->view('send_detail',$data);
    }	
	
	
	
	function company()
	{  
	    $this->top();
	    $total = $this->company->select('');
	    $config['base_url'] =  site_url('model/company/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 10; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['company'] = $this->company->select('',$config['per_page'],$page);
		$this->load->view('company',$data);
	}

//添加发放单位	
	function company_add(){
	  if($_POST){
	  	$resu = $this->company->insert();
	  	if($resu){
	  	  redirect('model/company');
	  	}
	  }else{	  	
	    $this->top();
		$this->load->view('company_add');
	  }
	}

//修改发放单位	
	function company_edit(){
	  $id = $this->uri->segment(3);
	  if($_POST){
	     $resu = $this->company->update($id); 
	     if($resu){
	  	  redirect('model/company');
	  	 }
	  }else{
	     
	     $company = $this->company->select($id);
	     $data['company'] = $company[0];
	     $data['id']=$id;
	     $this->top();
	     $this->load->view('company_add',$data);
	  }
	}

//删除发放单位
	function company_del(){
		$id = $this->uri->segment(3);
	    $resu = $this->company->del($id); 
	     if($resu){
	  	  redirect('model/company');
	  	 }
	}
	
	
//查看发放单位详情
    function company_detail(){
        $id = $this->uri->segment(3);
	    $company = $this->company->select($id);
	    $data['company'] = $company[0]; 
	    $this->top();
        $this->load->view('company_detail',$data);
    }	
    	
	
	function index()
	{  
	    $this->top();
	    $total = $this->mod->select('');
	    $config['base_url'] =  site_url('model/index/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 10; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['mod_del']=$this->session->userdata('mod_del');
		$data['mod'] = $this->mod->select('',$config['per_page'],$page);
		$this->load->view('model',$data);
	}

//添加型号	
	function model_add(){
		$this->session->set_userdata('mod_del',0);
	  if($_POST){
	  	$resu = $this->mod->insert();
	  	if($resu){
	  	  redirect('model/index');
	  	}
	  }else{	  	
	    $this->top();
		$this->load->view('model_add');
	  }
	}

//修改型号	
	function model_edit(){
	  $id = $this->uri->segment(3);
	  $this->session->set_userdata('mod_del',0);
	  if($_POST){
	     $resu = $this->mod->update($id); 
	     if($resu){
	  	  redirect('model/index');
	  	 }
	  }else{
	     
	     $mod = $this->mod->select($id);
	     $data['mod'] = $mod[0];
	     $data['id']=$id;
	     $this->top();
	     $this->load->view('model_add',$data);
	  }
	}

//删除型号	
	function model_del(){
		$id = $this->uri->segment(3);
		$re = $this->category->select(array('mod_id'=>$id));
		if(count($re)!=0){
		  $this->session->set_userdata('mod_del',1);
		  redirect('model/index');exit;
		}else{
		  $this->session->set_userdata('mod_del',0);
		}
	    $resu = $this->mod->del($id); 
	     if($resu){
	  	  redirect('model/index');
	  	 }
	}
	
	
//查看设备详情
    function model_detail(){
        $id = $this->uri->segment(3);
	    $mod = $this->mod->select($id);
	    $data['mod'] = $mod[0]; 
	    $this->top();
        $this->load->view('model_detail',$data);
    }	
	
    
//设备类别列表    
	function category()
	{  
	    $this->top();
	    if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('category_mod_id',$_POST['mod_id']);
	    }
	    $mod_id=$this->session->userdata('category_mod_id');
	    if($mod_id!=""){
	    	$where = array('mod_id'=>$mod_id);
	    }else{
	        $where = "";
	    }
	    	    	
	    $data['mod_id'] = $mod_id;
	    $data['mod'] = $this->mod->select('');

		$total = $this->category->select($where);
	    $config['base_url'] =  site_url('model/category/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 10; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['category_del']=$this->session->userdata('category_del');
		$data['category'] = $this->category->select($where,$config['per_page'],$page);
		$this->load->view('category',$data);
	}

//添加设备类别	
	function category_add(){
		$this->session->set_userdata('category_del',0);
	  if($_POST){
	  	$resu = $this->category->insert();
	  	if($resu){
	  	  redirect('model/category');
	  	}
	  }else{	  	
	    $this->top();
	    $data['mod'] = $this->mod->select('');
		$this->load->view('category_add',$data);
	  }
	}

//修改设备类别	
	function category_edit(){
	  $id = $this->uri->segment(3);
	  $this->session->set_userdata('category_del',0);
	  if($_POST){
	     $resu = $this->category->update($id); 
	     if($resu){
	  	  redirect('model/category');
	  	 }
	  }else{
	     
	     $category = $this->category->select(array('category_id'=>$id));
	     $data['category'] = $category[0];
	     $data['id']=$id;
	     $this->top();
	     $data['mod'] = $this->mod->select('');
	     $this->load->view('category_add',$data);
	  }
	}

//删除设备类别	
	function category_del(){
		$id = $this->uri->segment(3);
	    $re = $this->version->select(array('category_id'=>$id));
		if(count($re)!=0){
		  $this->session->set_userdata('category_del',1);
		  redirect('model/category');exit;
		}else{
		  $this->session->set_userdata('category_del',0);
		}
		
	    $resu = $this->category->del($id); 
	     if($resu){
	  	  redirect('model/category');
	  	 }
	}
	
	
//查看设备类别详情
    function category_detail(){
        $id = $this->uri->segment(3);
	    $category = $this->category->select(array('category_id'=>$id));
	    $data['category'] = $category[0]; 
	    $data['mod'] = $this->mod->select('');
	    $this->top();
        $this->load->view('category_detail',$data);
    }	

    
//型号资料列表
    function version(){
        $this->top();
        if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('version_mod_id',$_POST['mod_id']);
	    	$this->session->set_userdata('version_category_id',$_POST['category_id']);
	    }
	    $mod_id=$this->session->userdata('version_mod_id');
	    $category_id=$this->session->userdata('version_category_id');
	    if($mod_id!=""){
	    	$where = array('mod_id'=>$mod_id,'category_id'=>$category_id);
	    }else{
	        $where = "";
	    }
	    	    	
	    $data['mod_id'] = $mod_id;
	    $data['category_id'] = $category_id;
        $data['category'] = $this->category->select('');
	    $total = $this->version->select($where);
	    $config['base_url'] =  site_url('model/version/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 10; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['mod'] = $this->mod->select('');
		$version = $this->version->select($where,$config['per_page'],$page);
		for($i=0;$i<count($version);$i++){
		     $mod_id = $version[$i]->mod_id;
		     $category_id = $version[$i]->category_id;
		     $version_id = $version[$i]->version_id;
		     $count_send = $this->send_record->getlast($category_id,$version_id);
		     $count_receive = $this->receive_record->getlast($category_id,$version_id);
		     if($count_receive[0]->count==''){ $count = 0;}else{ $count = $count_receive[0]->count;}
		     $version[$i]->count = $count;
	//取出库存和已发放总分数     
		}
		$data['version']=$version;
		$this->load->view('version',$data);
    }	
    
//型号资料添加
     function version_add(){
        if($_POST){
          $result = $this->version->insert();
	        if($result){
		  	  redirect('model/version');
		  	}
        }else{
          $this->top();
          $data['mod'] = $this->mod->select('');
          $data['category'] = $this->category->select('');
		  $this->load->view('version_add',$data);
        }
     }
   
//修改型号资料
    function version_edit(){
      $id = $this->uri->segment(3);
	  if($_POST){
	  	 $da = array(
	  	              'mod_id'=>$_POST['mod_id'],
	  	              'des'=>$_POST['des'],
	  	              'category_id'=>$_POST['category_id'],
	  	              'name'=>$_POST['name'],
	  	             );
	     $resu = $this->version->update($da,$id); 
	     if($resu){
	  	  redirect('model/version');
	  	 }
	  }else{
	     
	     $version = $this->version->select(array('version_id'=>$id));
	     $data['version'] = $version[0];
	     $data['id']=$id;
	     $this->top();
	     $data['mod'] = $this->mod->select('');
	     $data['category'] = $this->category->select('');
	     $this->load->view('version_add',$data);
	  }
    }

//删除型号资料	
	function version_del(){
		$id = $this->uri->segment(3);
	    $resu = $this->version->del($id); 
	     if($resu){
	  	  redirect('model/version');
	  	 }
	}

//查看型号资料详情
    function version_detail(){
        $id = $this->uri->segment(3);
	    $version = $this->version->select(array('version_id'=>$id));
	    $data['mod'] = $this->mod->select('');
	    $data['version'] = $version[0]; 
	    $data['category'] = $this->category->select('');
	    $this->top();
        $this->load->view('version_detail',$data);
    }	

    
//成套资料列表
    function complete(){
        $this->top();
        if(isset($_POST['mod_id'])){
	    	$this->session->set_userdata('category_mod_id',$_POST['mod_id']);
	    }
	    $mod_id=$this->session->userdata('category_mod_id');
	    if($mod_id!=""){
	    	$where = array('mod_id'=>$mod_id);
	    }else{
	        $where = "";
	    }
	    	    	
	    $data['mod_id'] = $mod_id;
	    $data['mod'] = $this->mod->select('');
        $data['category'] = $this->category->select('');
	    $total = $this->complete->select($where);
	    $config['base_url'] =  site_url('model/complete/');
	    
		$config['total_rows'] = count($total);
		$config['per_page'] = 20; 
		$config['num_links'] = 10; 
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['full_tag_open'] = '<div>';
        $config['full_tag_close'] = '</div>';  
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();
		$page = $this->uri->segment(3);
		
		if($page==""){
		  $page=0;
		  $start=1;
		}else{
		  $start = $page+1;
		}
		$data['start']=$start;
		$data['complete'] = $this->complete->select($where,$config['per_page'],$page);
		$this->load->view('complete',$data);
    }	
    
//成套资料添加
     function complete_add(){
        if($_POST){
          $result = $this->complete->insert();
	      
        }else{
          $this->top();
          $data['category'] = $this->category->select('');
          $data['mod'] = $this->mod->select('');
		  $this->load->view('complete_add',$data);
        }
     }
   
//修改成套资料
    function complete_edit(){
      $id = $this->uri->segment(3);
	  if($_POST){
	     $resu = $this->complete->update($_POST['complete_id']); 
	    
	  }else{
	     
	     $complete = $this->complete->select(array('complete_id'=>$id));
	     $data['complete'] = $complete[0];
	     $data['id']=$id;
	     $this->top();
	     $data['mod'] = $this->mod->select('');
	     $mod_id = $complete[0]->mod_id;
	     $category_select =  $complete[0]->category_ids;
	     $category_ar = explode(',', $category_select);
	     $categorys = $this->category->select('');
	     $result = $this->category->getcategory_bymod_id($mod_id);
	     $count = count($categorys);
	     $str="";
	     if($count!=0){
	         for($i=0;$i<$count;$i++){
	           $str.="<p style='width:190px;float:left;'><input type='checkbox' name='category_ids[]' value='".$categorys[$i]->category_id."' ";
               if(in_array($categorys[$i]->category_id,$category_ar)){
                 $str.=" checked=true ";
               }
	           $str.=" />".$categorys[$i]->name." </p>";
	         }
	       $data['category'] = $str;      
	     }
	     $this->load->view('complete_add',$data);
	  }
    }

//删除成套资料	
	function complete_del(){
		$id = $this->uri->segment(3);
	    $resu = $this->complete->del($id); 
	     if($resu){
	  	  redirect('model/complete');
	  	 }
	}

//查看成套资料详情
    function complete_detail(){
        $id = $this->uri->segment(3);
	    $complete = $this->complete->select(array('complete_id'=>$id));
	    $data['complete'] = $complete[0]; 
	    $data['mod'] = $this->mod->select($complete[0]->mod_id);
	    $data['category'] = $this->category->select('');
	    $this->top();
        $this->load->view('complete_detail',$data);
    }	 

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */